﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Models.Common;
using Models.WebClient;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using UtilsORM;
using UtilsORM.MySqlData;

////using System.Web.Script.Serialization;

namespace ServiceWeb.DBGateway
{
    public class DBIotDeviceTaskPlainHandle
    {



        public void setPlainItemHandleValue(string tpitemGuid, string val)
        {

            EFYihongIotContext ef = new EFYihongIotContext();
            iot_device_task_plain_item_handle handle = ef.iot_device_task_plain_item_handle.Where(c => c.tpi_guid == tpitemGuid).FirstOrDefault();
            if (handle != null)
            {

                if (handle.tpi_key == "delay" && handle.tpi_execute_delayed_time != null)
                {
                    //重设到期时间
                    DateTime tpi_execute_delayed_time =DateTime.Parse(handle.tpi_execute_time.ToString()).AddSeconds(double.Parse(val));
                    ef.iot_device_task_plain_item_handle.Where(c => c.tpi_guid == tpitemGuid).ExecuteUpdate(c => c.SetProperty(p => p.tpi_value, val)
                    .SetProperty(p => p.tpi_execute_delayed_time, tpi_execute_delayed_time));
                }
                else
                {
                    ef.iot_device_task_plain_item_handle.Where(c => c.tpi_guid == tpitemGuid).ExecuteUpdate(c => c.SetProperty(p => p.tpi_value, val));
                }
            }
        }
    }
}
